﻿<?php
/* * ***************************************************************************
 * To access this WSDL specification run via: /wsdl.php?WSDL
 * Any other access to this WSDL will display as a HTML document
 * 
 * 2013 (C) Copyright Lyndon Leverington / DarkerWhite
 * ****************************************************************************
 * Set up the web service parameters:
 * $serviceName: Plain Text to display when someone accesses this service 
 *               without the ?WSDL parameter in the URL. Whitespaces are
 *               removed from this and this is then used as the ID for the 
 *               XML in the WSDL. Please only use A-Z, 0-9 and spaces.
 *
 * Declare all Functions for this Web Service
 * $functions Array Parameters:
 *  funcName - Name of the particular function being served
 *  doc - Documentation to report from Web Service regarding this function
 *  inputParams - An array of arrays where name = name of field and type = data type
 *                  Omit if not required
 *  outputParams - As above, but for responses
 *                  Omit if not required
 *  soapAddress - The php file to send to to process SOAP requests                
 * *************************************************************************** */

// ----------------------------------------------------------------------------
// END OF PARAMETERS SET UP
// ----------------------------------------------------------------------------

/* * ***************************************************************************
 * Process Page / Request
 * *************************************************************************** */
class soap_wsdl {

    public $types = array();
    public $functions = array();
    public $url;

    public function __construct($url) {
        $this->url = $url;
    }

    public function __get($name) {
        return $this->$name;
    }

    public function __set($name, $value) {
        $this->$name = $value;
    }

    public function addComplexType($typeName, $data) {
        $this->types[] = array("name" => $typeName, "data" => $data);
    }

    public function configWSDL() {
        $GLOBALS['functions'] = $this->functions;
        $GLOBALS['url'] = $this->url;
        $GLOBALS['types'] = $this->types;
    }

    public function DisplayXML($xmlformat = true) {
        global $functions;         // Functions that this web service supports
        global $serviceName;       // Web Service ID
        global $url;
        global $types;
        $i = 0;                    // For traversing functions array
        $j = 0;                    // For traversing parameters arrays
        $str = '';                 // XML String to output
        // Tab spacings
        $t1 = '    ';
        if (!$xmlformat)
            $t1 = '&nbsp;&nbsp;&nbsp;&nbsp;';
        $t2 = $t1 . $t1;
        $t3 = $t2 . $t1;
        $t4 = $t3 . $t1;
        $t5 = $t4 . $t1;

        $serviceID = str_replace(" ", "", $serviceName);

        // Declare XML format
        $str .= "<?xml version ='1.0' encoding ='ISO-8859-1' ?>" . "\n\n";

        // Declare definitions / namespaces
        $str .= "<definitions targetNamespace='http://localhost/soap" . $url . "'" . "\n";
        $str .= $t1 . "xmlns:tns='http://localhost/soap" . $url . "'" . "\n";
        $str .= $t1 . "xmlns:SOAP-ENV='http://schemas.xmlsoap.org/soap/envelope/'" . "\n";
        $str .= $t1 . "xmlns:soap='http://schemas.xmlsoap.org/wsdl/soap/'" . "\n";
        $str .= $t1 . "xmlns:xsd='http://www.w3.org/2001/XMLSchema'" . "\n";
        $str .= $t1 . "xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance'" . "\n";
        $str .= $t1 . "xmlns:SOAP-ENC='http://schemas.xmlsoap.org/soap/encoding/'" . "\n";
        $str .= $t1 . "xmlns:wsdl='http://schemas.xmlsoap.org/wsdl/'" . "\n";
        $str .= $t1 . "xmlns='http://schemas.xmlsoap.org/wsdl/'" . "\n";
        $str .= '>' . "\n\n";

        // Declare Types / Schema
        $str .= "<types>" . "\n";
        $str .= $t1 . '<xsd:schema targetNamespace="http://localhost/soap' . $url . '">' . "\n";
        $str .= $t2 . '<xsd:import namespace="http://schemas.xmlsoap.org/soap/encoding/" />' . "\n";
        $str .= $t2 . '<xsd:import namespace="http://schemas.xmlsoap.org/wsdl/" />' . "\n";
        for ($i = 0; $i < count($types); $i++) {
            // Define Types
            $str .= $t2 . '<xsd:complexType name="' . $types[$i]['name'] . '">' . "\n";
            $str .= $t3 . '<xsd:' . $types[$i]['name'] . '>' . "\n";
            foreach ($types[$i]['data'] as $key => $value) {
                $str .= $t4 . '<xsd:element ';
                $str .= 'name="' . $types[$i]['data'][$key]['name'] . '" ';
                $str .= 'type="' . $types[$i]['data'][$key]['type'] . '" />' . "\n";
            }
            $str .= $t3 . '</xsd:' . $types[$i]['name'] . '>' . "\n";
            $str .= $t2 . '</xsd:complexType>' . "\n";
        }
        $str .= $t1 . '</xsd:schema>' . "\n";
        $str .= '</types>' . "\n\n";

        // Declare Messages
        for ($i = 0; $i < count($functions); $i++) {
            // Define Request Messages
            if (array_key_exists("inputParams", $functions[$i])) {
                if (count($functions[$i]['inputParams']) > 0) {
                    for ($j = 0; $j < count($functions[$i]['inputParams']); $j++) {
                        $str .= '<message name="' . $functions[$i]['funcName'] . 'Request">' . "\n";
                        $str .= $t1 . '<part name="' . $functions[$i]['inputParams'][$j]['name'] . '" type="' . $functions[$i]['inputParams'][$j]['type'] . '" />' . "\n";
                        $str .= '</message>' . "\n";
                    }
                } else {
                    $str .= '<message name="' . $functions[$i]['funcName'] . 'Request" />' . "\n";
                }
            }
            // Define Response Messages
            if (array_key_exists("outputParams", $functions[$i])) {
                for ($j = 0; $j < count($functions[$i]['outputParams']); $j++) {
                    $str .= '<message name="' . $functions[$i]['funcName'] . 'Response">' . "\n";
                    $str .= $t1 . '<part name="' . $functions[$i]['outputParams'][$j]['name'] . '" type="' . $functions[$i]['outputParams'][$j]['type'] . '" />' . "\n";
                    $str .= '</message>' . "\n\n";
                }
            }
        }

        // Declare Port Types
        for ($i = 0; $i < count($functions); $i++) {
            $str .= '<portType name="' . $url . 'PortType">' . "\n";
            $str .= $t1 . '<operation name="' . $functions[$i]['funcName'] . '">' . "\n";
            if (array_key_exists("inputParams", $functions[$i]))
                $str .= $t2 . '<input message="tns:' . $functions[$i]['funcName'] . 'Request" />' . "\n";
            if (array_key_exists("outputParams", $functions[$i]))
                $str .= $t2 . '<output message="tns:' . $functions[$i]['funcName'] . 'Response" />' . "\n";
            $str .= $t1 . '</operation>' . "\n";
            $str .= '</portType>' . "\n\n";
        }

        // Declare Bindings
        $str .= '<binding name="' . $url . 'Binding" type="tns:' . $url . 'PortType">' . "\n";
        $str .= $t1 . '<soap:binding style="rpc" transport="http://schemas.xmlsoap.org/soap/http" />' . "\n";
        for ($i = 0; $i < count($functions); $i++) {
            $str .= $t1 . '<operation name="' . $functions[$i]['funcName'] . '">' . "\n";
            $str .= $t2 . '<soap:operation soapAction="' . $functions[$i]['soapAddress'] . '/' . $functions[$i]['funcName'] . '" style="rpc" />' . "\n";
            if (array_key_exists("inputParams", $functions[$i]))
                $str .= $t2 . '<input><soap:body use="encoded" namespace="" encodingStyle="http://schemas.xmlsoap.org/soap/encoding/"/></input>' . "\n";
            if (array_key_exists("outputParams", $functions[$i]))
                $str .= $t2 . '<output><soap:body use="encoded" namespace="" encodingStyle="http://schemas.xmlsoap.org/soap/encoding/" /></output>' . "\n";
            $str .= $t1 . '</operation>' . "\n";
        }
        $str .= '</binding>' . "\n\n";

        // Declare Service
        $str .= '<service name="' . $url . '">' . "\n";
        $str .= $t1 . '<port name="' . $url . 'Port" binding="tns:' . $url . 'Binding">' . "\n";
        $str .= $t2 . '<soap:address location="' . $functions[0]['soapAddress'] . '" />' . "\n";
        $str .= $t1 . '</port>' . "\n";
        $str .= '</service>' . "\n\n";

        // End Document
        $str .= '</definitions>' . "\n";

        if (!$xmlformat)
            $str = str_replace("<", "&lt;", $str);
        if (!$xmlformat)
            $str = str_replace(">", "&gt;", $str);
        if (!$xmlformat)
            $str = str_replace("\n", "<br />", $str);
        return $str;
    }
}
?>